package com.claude.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.claude.bean.Cron;
import com.claude.bean.Session;
import com.claude.bean.Student;
import com.claude.bean.Subject;
import com.claude.bean.User;
import com.claude.bean.Version;

import java.util.ArrayList;

/**
 * Created by Claude on 2014/12/24.
 */
public class Parser {
	public static Version getVersion(JSONObject jsonObject){
		try{
			Version version = new Version();
			version.setImportance(jsonObject.getString("importance"));
			version.setUrl(jsonObject.getString("url"));
			version.setVersion(jsonObject.getString("version"));
			return version;
		}catch(JSONException error){
			error.printStackTrace();
			return null;
		}
	}
	
	public static Cron getCron(JSONObject jsonObject){
		try{
			Cron cron = new Cron();
			cron.setLessonNumber(jsonObject.getString("lessonNumber"));
			cron.setClassroom(jsonObject.getString("classroom"));
			cron.setStartTime(jsonObject.getString("startTime"));
			cron.setEndTime(jsonObject.getString("endTime"));
			return cron;
		}catch(JSONException error){
			error.printStackTrace();
			return null;
		}
	}
	
    public static User getUser(JSONObject jsonObject){
        try {
            User user = new User();
            user.setTeacherNumber(jsonObject.getString("teacherNumber"));
            user.setDepartmentNumber(jsonObject.getString("departmentNumber"));
            user.setName("name");
            return user;
        }catch (JSONException error){
            error.printStackTrace();
            return null;
        }
    }

    public static ArrayList<Subject> getSubjects(JSONObject jsonObject){
        try {
            ArrayList<Subject> subjects = new ArrayList<Subject>();
            JSONArray jsonSubject = jsonObject.getJSONArray("subjectList");
            int index = 0;
            while(true){
                JSONObject tmpObject = (JSONObject)jsonSubject.get(index);
                Subject tmpSubject = new Subject();
                tmpSubject.setName(tmpObject.getString("name"));
                tmpSubject.setSubjectNumber(tmpObject.getString("subjectNumber"));
                subjects.add(tmpSubject);
                index++;
                if(index == jsonSubject.length()){
                    break;
                }
            }
            return subjects;
        }catch (JSONException error){
            error.printStackTrace();
            return null;
        }
    }
    
    public static ArrayList<Session> getClasses(JSONObject jsonObject){
        try {
            ArrayList<Session> sessions = new ArrayList<Session>();
            JSONArray jsonSubject = jsonObject.getJSONArray("sessionList");
            int index = 0;
            while(true){
                JSONObject tmpObject = (JSONObject)jsonSubject.get(index);
                Session tmpSession = new Session();
                tmpSession.setSessionNumber(tmpObject.getString("sessionNumber"));
                tmpSession.setClassName(tmpObject.getString("className"));
                tmpSession.setStartWeek(tmpObject.getString("startWeek"));
                tmpSession.setEndWeek(tmpObject.getString("endWeek"));
                sessions.add(tmpSession);
                index++;
                if(index == jsonSubject.length()){
                    break;
                }
            }
            return sessions;
        }catch (JSONException error){
            error.printStackTrace();
            return null;
        }
    }
    
    public static ArrayList<Student> getStudent(JSONObject jsonObject){
    	try {
            ArrayList<Student> students = new ArrayList<Student>();
            JSONArray jsonSubject = jsonObject.getJSONArray("studentList");
            int index = 0;
            while(true){
                JSONObject tmpObject = (JSONObject)jsonSubject.get(index);
                Student tmpStudent = new Student();
                tmpStudent.setCardID(tmpObject.getString("cardID"));
                tmpStudent.setCardMac(tmpObject.getString("cardMac"));
                tmpStudent.setEnterYear(tmpObject.getString("enterYear"));
                tmpStudent.setName(tmpObject.getString("name"));
                tmpStudent.setProfession(tmpObject.getString("profession"));
                tmpStudent.setSex(tmpObject.getString("sex"));
                tmpStudent.setStudentNumber(tmpObject.getString("studentNumber"));
                students.add(tmpStudent);
                index++;
                if(index == jsonSubject.length()){
                    break;
                }
            }
            return students;
        }catch (JSONException error){
            error.printStackTrace();
            return null;
        }
    }
}
